package com.huiminpay.merchant.controller;


import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.api.IAppService;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.merchant.covert.AppCovert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.vo.AppVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "应用操作相关", tags = {"应用操作相关"}, description = "应用操作相关包括CRUD")
@RestController
public class AppController {

    @Reference
    IAppService iAppService;

    @ApiOperation("商户创建应用")
    @ApiImplicitParam(name = "appVo",value = " 应用信息",
            required = true, dataType = "AppVo", paramType = "body")
    @PostMapping("/my/apps")
    public AppVo createApp(@RequestBody AppVo appVo){
        //非空验证
        if (StringUtil.isEmpty(appVo.getAppName())){
            throw new BusinessException(CommonErrorCode.E_300001);
        }

        AppDTO appDTO = AppCovert.INSTANCE.vo2dto(appVo);
        appDTO.setMerchantId(SecurityUtil.getMerchantId());
        AppDTO appDTOBack = iAppService.insertApp(appDTO);

        return AppCovert.INSTANCE.dto2vo(appDTOBack);
    }


    @ApiOperation("根据业务ID（appID）")
    @ApiImplicitParam(name = "appID", value = "应用ID",
            required = true, dataType = "String", paramType = "path")
    @GetMapping("my/apps/{appID}")
    public AppVo queryAppByID(@PathVariable String appID){
        //非空验证
        if (StringUtil.isEmpty(appID)){
          throw new BusinessException(CommonErrorCode.E_300003);
        }

        AppDTO appDTO = iAppService.queryAppByID(appID);

        return AppCovert.INSTANCE.dto2vo(appDTO);

    }


    @ApiOperation("商户查询我的应用")
    @GetMapping("my/apps")
    public List<AppVo> queryApps(){

        //获得当前登录商户ID
        Long merchantId = SecurityUtil.getMerchantId();
        //根据商户ID查询多个应用信息
        List<AppDTO> appDTOS = iAppService.queryApps(merchantId);

        return AppCovert.INSTANCE.dto2voList(appDTOS);

    }



}
